//
//  HBTextAttachment.swift
//  sinaWeibo
//
//  Created by Beans on 16/7/3.
//  Copyright © 2016年 iceWorks. All rights reserved.
//

import UIKit

class HBTextAttachment: NSTextAttachment {

    var emotion: HBEmotion?

    // 对象方法
    func getAttributeString(font: UIFont) -> NSAttributedString? {

        // 1. 创建附件对象
        let attachment = NSTextAttachment()
        // 1.1 拼接路径
        guard let path = emotion?.path, png = emotion?.png else {
            return nil
        }
        let imagePath = "\(path)/\(png)"
        // 1.2 设置属性
        attachment.image = UIImage(named: imagePath)
        // 1.3 修改图片bounds
        let lineHeight = font.lineHeight
        attachment.bounds = CGRectMake(0, -5, lineHeight, lineHeight)  // y的负值向下

        // 2. NSAttributeString对象
        let attribute = NSAttributedString(attachment: attachment)

        return attribute
    }

    // 类方法 (不能以属性方式传递HBEmotion, 定义为参数)
    class func getAttributeString(oEmotion: HBEmotion?, font: UIFont) -> NSAttributedString? {
        // 1. 创建附件对象
        let attachment = NSTextAttachment()
        // 1.1 拼接路径
        guard let path = oEmotion?.path, png = oEmotion?.png else {
            return nil
        }
        let imagePath = "\(path)/\(png)"
        // 1.2 设置属性
        attachment.image = UIImage(named: imagePath)
        // 1.3 修改图片bounds
        let lineHeight = font.lineHeight
        attachment.bounds = CGRectMake(0, -5, lineHeight, lineHeight)  // y的负值向下

        // 2. NSAttributeString对象
        let attribute = NSAttributedString(attachment: attachment)

        return attribute
    }
}
